package com.socialuni.jimei.config;

import com.socialuni.api.model.ResultRO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

@RestControllerAdvice
@Slf4j
public class WebControllerAdvice {

    /**
     * 全局异常捕捉处理
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    @ResponseStatus()
    public ResultRO<?> errorHandler(Exception ex) {
        ResultRO<?> resultRO = new ResultRO<>(500, ex.getMessage());
        ex.printStackTrace();
        return resultRO;
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    @ResponseStatus(value = HttpStatus.NOT_FOUND)
    public ResultRO<?> handle404(NoHandlerFoundException ex) {
        ResultRO<?> resultRO = new ResultRO<>(404, ex.getMessage());
        ex.printStackTrace();
        return resultRO;
    }
}
